


import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import java.net.URL;
import java.sql.*;

public class ChangeGrade extends JFrame implements ActionListener{
	

	JPanel jp_1, jp_2 = null;
	JLabel jlb = null;
	JTextField jtf = null;
	JButton jb_1, jb_2, jb_3 = null;
	

	String Sno;
	String subject;

	static String dbURL = "jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false";


	static String userName="root";
	static String userPwd="0118";

			

	static Connection ct = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	



	public void clear(){
		jtf.setText("");
	}
	

	public ChangeGrade(String sno, String sub){

		Sno = sno;
		subject = sub;
		

		try {
			DataSource dataSource = new MysqlDataSource();
			((MysqlDataSource) dataSource).setURL(dbURL);
			((MysqlDataSource) dataSource).setUser(userName);
			((MysqlDataSource) dataSource).setPassword(userPwd);
			ct = dataSource.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		jp_1 = new JPanel();
		jp_2 = new JPanel();
		

		jlb = new JLabel("请输入修改后成绩");
		jlb.setFont(new java.awt.Font("Dialog", 1, 20));
		

		jtf = new JTextField(10);
		

		jb_1 = new JButton("确认修改");
		jb_2 = new JButton("重置");
		jb_3 = new JButton("退出修改");
		jb_1.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_2.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_3.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_2.setBackground(Color.GRAY);
		jb_1.setBackground(Color.GRAY);
		jb_3.setBackground(Color.GRAY);


		jb_1.addActionListener(this);
		jb_2.addActionListener(this);
		jb_3.addActionListener(this);
		

		jp_1.add(jlb);
		jp_1.add(jtf);
		
		jp_2.add(jb_1);
		jp_2.add(jb_2);
		jp_2.add(jb_3);
		

		this.add(jp_1);
		this.add(jp_2);
		

		this.setLayout(new GridLayout(2,1));

		this.setBackground(Color.yellow);
		this.setTitle("修改学生成绩");

		URL resource = Login.class.getClassLoader().getResource("河池学院.jfif");
		Image image = new ImageIcon(resource).getImage();
		this.setIconImage(image);
						

		this.setSize(500, 300);
						

		this.setLocationRelativeTo(null);
						

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						

		this.setVisible(true);
		this.setResizable(true);				
	}
	

	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand() == "确认修改"){
			change_grade();
		}else if(e.getActionCommand() == "重置"){
			clear();
		}else if(e.getActionCommand() == "退出修改"){
			dispose();
		}
	}
	
	public void change_grade(){
		String new_grade = String.valueOf(jtf.getText());

		double num_grade = 0;
		if(!new_grade.isEmpty())
			num_grade = Double.valueOf(new_grade);
		
		if(new_grade.isEmpty()){
			JOptionPane.showMessageDialog(null, "修改成绩为空\n请重新输入！", "提示信息", JOptionPane.WARNING_MESSAGE);
		}else if(num_grade > 100 || num_grade < 0){
			JOptionPane.showMessageDialog(null, "分数必须在0~100之间\n请重新输入！", "提示信息", JOptionPane.WARNING_MESSAGE);
		}else {
			try{
				String sql;
				if(subject.equals("高数")) {
					sql = "update Student set 高数=? where 学号=?";
					ps = ct.prepareStatement(sql);
				}else if(subject.equals("C语言")) {
					sql = "update Student set C语言 = ? where 学号=?";
					ps = ct.prepareStatement(sql);
				}else if(subject.equals("Java")) {
					sql = "update Student set java=? where 学号=?";
					ps = ct.prepareStatement(sql);
				} else if(subject.equals("英语")){
					sql = "update Student set 英语=? where 学号=?";
					ps = ct.prepareStatement(sql);
				}
				ps.setString(1, new_grade);
				ps.setString(2, Sno);
				int count=ps.executeUpdate();
				ct.close();
			}
			catch (SQLException e1) {
				e1.printStackTrace();
			}	
			JOptionPane.showMessageDialog(null, "修改成功", "提示信息", JOptionPane.WARNING_MESSAGE);
			dispose();
		}
	}
}

